﻿#include "glserver.h"

GLServer *GLServer::instance = nullptr;

GLServer *GLServer::getInstance()
{
    if (!instance) {
        instance = new GLServer();
    }
    return instance;
}

void GLServer::destroy()
{
    if (instance) {
        delete instance;
        instance = nullptr;
    }
}

GLServer::~GLServer()
{

}

void GLServer::setColor(GLServer::ColorVector type, QVector4D color)
{
    switch (type) {
    case LINE_COLOR:
        lineColor = color;
        break;
    default:
        break;
    }
}

QVector4D GLServer::getColor(GLServer::ColorVector type)
{
    QVector4D output;
    switch (type) {
    case LINE_COLOR:
        output = lineColor;
        break;
    default:
        break;
    }
    return output;
}

int GLServer::getBackVertexSize()
{
    return backgroundVertex.size() / 2;
}

GLfloat *GLServer::getBackVertexData()
{
    return backgroundVertex.data();
}

int *GLServer::getScanDataFromFile()
{
    QFile file("./source.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream stream(&file);
        int *data = new int[10000];
        int *temp = data;
        for (int i = 0; i < 10000; i++) {
            stream >> *temp;
            temp++;
        }
        return data;
    } else {
        return nullptr;
    }
}

GLfloat *GLServer::getScanVertexData()
{
    int *source = getScanDataFromFile();
    if (source) {
        GLfloat *data = new GLfloat[20000];
        GLfloat step = 2.0 / (10000 - 1);
        GLfloat *dataTemp = data;
        int *sourceTemp = source;
        for (int i = 0; i < 10000; i++) {
            *(dataTemp++) = step * i - 1.0;
            *(dataTemp++) = 2.0 / 256 * (*sourceTemp++) - 1.0;
        }
        delete []source;
        return data;
    } else {
        return nullptr;
    }
}

GLServer::GLServer()
{
    lineColor = QVector4D(1.0, 1.0, 0.0, 1.0);
    backgroundVertex.clear();
    for (int i = 1; i < LARGE_SECTION_X; i++) {
        GLfloat xPos = 2.0 * i / LARGE_SECTION_X - 1.0;
        GLfloat yPos = 0.0;
        for (int j = 1; j < LARGE_SECTION_Y; j++) {
            yPos = 2.0 * j / LARGE_SECTION_Y - 1.0;
            backgroundVertex.push_back(xPos);
            backgroundVertex.push_back(yPos);
        }
    }

    for (int i = 1; i < LARGE_SECTION_X; i++) {
        GLfloat xPos = 2.0 * i / LARGE_SECTION_X - 1.0;
        GLfloat yPos = 0.0;
        int section = LARGE_SECTION_Y * LITTLE_SECTION_Y;
        for (int j = 0; j < LARGE_SECTION_Y; j++) {
            for (int k = 1; k < LITTLE_SECTION_Y; k++) {
                yPos = 2.0 * (j * LITTLE_SECTION_Y + k) / section - 1.0;
                backgroundVertex.push_back(xPos);
                backgroundVertex.push_back(yPos);
            }
        }
    }

    for (int i = 1; i < LARGE_SECTION_Y; i++) {
        GLfloat yPos = 2.0 * i / LARGE_SECTION_Y - 1.0;
        GLfloat xPos = 0.0;
        int section = LARGE_SECTION_X * LITTLE_SECTION_X;
        for (int j = 0; j < LARGE_SECTION_X; j++) {
            for (int k = 1; k < LITTLE_SECTION_X; k++) {
                xPos = 2.0 * (j * LITTLE_SECTION_X + k) / section - 1.0;
                backgroundVertex.push_back(xPos);
                backgroundVertex.push_back(yPos);
            }
        }
    }
}

GLServer::GLServer(const GLServer &origin)
{
    lineColor = origin.lineColor;
    backgroundVertex = origin.backgroundVertex;
}
